/*
 * @(#)ListAllIdInfoITCase.java
 * Date 2014-01-20
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package com.chrisuyehara.vista.rpc.procedures.ORWPT;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import com.chrisuyehara.vista.rpc.client.TestHarness;

import com.chrisuyehara.vista.rpc.client.RPCClient;

import com.chrisuyehara.vista.rpc.exceptions.LoginException;

import com.chrisuyehara.vista.rpc.models.PatientIdInfo;

import com.chrisuyehara.vista.rpc.procedures.VAFC.ConvertDFNToICN;

import org.junit.Assert;
import org.junit.Test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Class description
 *
 *
 * @version        v1.0, 2014-01-20
 * @author         Jim Horner
 */
public class ListAllIdInfoITCase {

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Method description
     *
     *
     * @throws Exception
     */
    @Test
    public void listAllIdInfoTest() throws Exception {

        List<PatientIdInfo> patients = new ArrayList<>();
        RPCClient c = new RPCClient(TestHarness.VISTA_HOSTNAME, TestHarness.VISTA_PORT);

        try {
            c.login(TestHarness.ACCESS_CODE, TestHarness.VERIFY_CODE);

            ListAll listall = new ListAll("NINETY", "1");

            c.call(listall);
            PatientIdInfo[] resp = listall.getPatients();

            for (int i = 0; i < Math.min(5, resp.length); ++i) {

                PatientIdInfo dfninfo = resp[i];
                String dfn = dfninfo.getDfn();

                IdInfo idinfo = new IdInfo(dfn);

                c.call(idinfo);

                logger.debug("IdInfo {}", idinfo.getResponse());
                PatientIdInfo info = idinfo.getPatientIdInfo();

                info.setDfn(dfninfo.getDfn());

                ConvertDFNToICN dfn2icn = new ConvertDFNToICN(dfn);

                c.call(dfn2icn);

                info.setIcn(dfn2icn.getICN());

                patients.add(info);
            }

        } catch (LoginException e) {

            Assert.fail("Login shouldn't have failed");

        } catch (IOException e) {

            Assert.fail("Shouldn't have an IO failure");

        } finally {

            if (c.isConnected()) {

                c.disconnect();
            }
        }

        logger.debug("Found {}", patients.size());
        for (PatientIdInfo info : patients) {

            logger.debug("{}^{}^{}^{}^{}^{}^{}", new Object[] {

                info.getIcn(), info.getDfn(), info.getSsn(), info.getDateOfBirth(),
                info.getGender(), info.getLastName(), info.getFirstName()

            });
        }
    }
}
